<template>
	<view class="container-root">
		<input class='password-change-input' placeholder='原密码' password='true' @input ='inputOriginalPwd'></input>
		  
		<input class='password-change-input' placeholder='新密码' password='true' @input ='inputNewPwd'></input>
		
		<button class='password-change-button' hover-class='password-change-button-hover' @click='onSetting'>确认</button>
	</view>
</template>

<script>
	import {
			mapState,
			mapMutations
	} from 'vuex'
	import apis from '@/api.js'
	import httpRequest from '@/utils/utilHttp.js'
	
	export default {
		data() {
			return {
				passWordType:'',
				oldPassWord:'',
				newPassWord:''
			}
		},
		onLoad(options){
			
			this.passWordType = options.passwordType;
			uni.setNavigationBarTitle({
				title:options.passwordType === '0' ? "修改登录密码" : "修改支付密码",
			})
		},		
		computed:{
			...mapState(['token', 'userInfo']),
		},
		methods: {
			inputOriginalPwd(e){
				this.oldPassWord = e.detail.value;
			},
			inputNewPwd(e){
				this.newPassWord = e.detail.value;
			},
			settingLoginPassWord:function(oldPwd, newPwd){
				var self = this;
				let params = {
				  'userCode': self.userInfo.userCode,
				  'token':self.token,
				  'password': newPwd,
				  'oldpwd': oldPwd
				}
				let options = {
					url:httpRequest.requestUrl.changePwd_post,
					method:httpRequest.requestMethod.POST,
					datas:params
				}
				httpRequest.Request(options).then(res => {
					//console.log(res)
					if (res.data.status == 200) {
						uni.showToast({
						    title: "密码修改成功！",
						    icon: "success"
						})
						setTimeout(function(){
							uni.navigateBack({delta:1})
						},1000)
						// uni.navigateBack({});
					}
					else if (res.data.status == '40098'){
						uni.showToast({
						    title: "原密码验证错误！",
						    icon: "none"
						})
					}
					else{
						uni.showToast({
						    title: "密码修改失败！",
						    icon: "none"
						})
					}
				})
			},
			onSetting(){
				let self = this;
				if(self.oldPassWord.length === 0)
				{
					uni.showToast({
						title: '原密码不能为空!',
						icon: "none"
					});					
				}
				else if(self.newPassWord.length === 0){					
					uni.showToast({
						title: '新密码不能为空!',
						icon: "none"
					});	
				}
				else if(self.oldPassWord === self.newPassWord){					
					uni.showToast({
						title: '原密码和新密码不能相同!',
						icon: "none"
					});
				}
				else{
					self.settingLoginPassWord(self.oldPassWord, self.newPassWord);
				}
			}
		}
	}
</script>

<style>
	page{
		background-color: #F1F1F1;
	}
	.password-change-input{
		width: calc(100% - 60rpx);
		height: 90rpx;
		border-radius: 15rpx;
		background-color: white;
		margin-top: 20rpx;
		padding: 0rpx 10rpx 0rpx 10rpx;
	}
	.password-change-button{
		width: calc(100% - 40rpx);
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;		
		margin-top: 20rpx;
		background-color: #00b9ef;
		color: white;
	}
	.password-change-button::after{
		border: none;
	}
	.password-change-button-hover{
		background-color: rgba(0, 185, 239, 0.7); 
	}
</style>
